layui.define(['table', 'form','my_send'], function(exports){
  var $ = layui.$
  ,table = layui.table
  ,form = layui.form
  , http_proxy = layui.http_proxy;
  function tdTitle(){
    $('th').each(function(index,element){
        $(element).attr('title',$(element).text());
    });
    $('td').each(function(index,element){
        $(element).attr('title',$(element).text());
    });
};
  //车站下拉列表
  $.ajax({
    type : "get",
    async : false, //异步执行
    url : http_proxy("/devStatus/getStation"),
    dataType : "json", //返回数据形式为json
    success : function(data) {
      $.each(data.result,function(index,item){
        $('#stationList').append(new Option(item.label,item.label));
      })
      form.render('select');
    },
    error : function(errorMsg) {
      alert("请求数据失败");
    }
  });
  //处理状态
  $.ajax({
    type : "get",
    async : false, //异步执行
    url : http_proxy("/devStatus/getAlarmLevel"),
    dataType : "json", //返回数据形式为json
    success : function(data) {
      $.each(data.result,function(index,item){
        $('#alarmLevels').append(new Option(item.label,item.value));
      })
      form.render('select');

    },
    error : function(errorMsg) {
      alert("请求数据失败");
    }
  });
    // 报警综合信息
    table.render({
      elem: '#alarmComprehensive'
      , url: http_proxy('/alertManager/alarmSynMsg') //模拟接口
      , method: 'post'
      , contentType : "application/json"
      ,request: {
        pageName: 'pageNo', //页码的参数名称，默认：page
        limitName: 'pageSize' //每页数据量的参数名，默认：limit
      }
      ,parseData:function(res){
        return{
          "code":0
          ,"count":res.total
          ,"data":res.rows
          ,'msg':''
        }
      }
      ,cols: [
        [
        {field: 'ehid',width: 80,title: '工单号',align: 'center'}
        ,{field: 'alRank',width: 120,title: '报警等级',align: 'center',templet:'#alarmRank'}
        ,{field: 'alStation',title: '车站名称',align: 'center'}
        ,{field: 'alSystem',title: '系统名称',align: 'center'}
        ,{field: 'deviceCn',title: '设备名称',align: 'center'}
        ,{field: 'alMsg',title: '描述',align: 'center'}
        ,{field: 'regainStatu',width: 80,title: '状态',align: 'center',templet:'#regainStatu'}
        ,{field: 'alTime',width: 180,title: '时间',align: 'center'}
        ,{title: '操作', width: 120, align:'center', fixed: 'right', toolbar: '#detailOperate'}
      ]
    ]
      ,page: true
      ,limit: 10
      ,limits: [10, 15, 20, 25, 30]
      ,text: {
        none:'暂无数据'
      }
      ,done:function(res){
        tdTitle();
      }
    });
     //监听工具条
  table.on('tool(alarmComprehensive)', function(obj){
    var data = obj.data;
    if(obj.event === 'detail'){
      layer.open({
        type: 2
        ,title: '设备故障报警'
        ,content: 'alarmComprehensiveDetail.html?ehid='+ data.ehid
        ,maxmin: true
        ,area: ['90%', '90%']
        ,btn: ['确定', '取消']
        ,success: function(layero, index){
          let iframe = window['layui-layer-iframe'+index];
          iframe.initData(data);
        }
        ,yes: function(index, layero){
          var iframeWindow = window['layui-layer-iframe'+ index]
          ,submit = layero.find('iframe').contents().find("#layuiadmin-app-form-edit");
          //监听提交
          iframeWindow.layui.form.on('submit(layuiadmin-app-form-edit)', function(data){
            var field = data.field; //获取提交的字段
            $.ajax({
              url:http_proxy("/equipmentLAIM/saveAttrValue"),
              type:'post',
              data:field,
              success:function(data){
                if(data.code=='success' ){
                  layer.msg(data.message, {icon: 6});

                }else{
                  layer.msg(data.message, {icon: 5});
                }
              }
            });
            window.location.reload();

          });
          submit.trigger('click');
        }
      });
    }
  });
   //权限管理
   table.render({
    elem: '#LAY-user-manage'
    ,url: http_proxy('/role/getRoleByParams') //模拟接口
    ,parseData:function(res){
      return{
        "code":0
        ,"count":res.total
        ,"data":res.rows
        ,'msg':''
      }
    }
    , cols: [[ //表头
      { type: 'numbers', title: '序号', width: '5%', fixed: 'left', unresize: true }
      , { field: 'urid', hide: true }
      , { field: 'roleName', title: '名称', width: '20%', align: 'center', unresize: true }
      , { field: 'roleCode', title: 'code', width: '20%', align: 'center', unresize: true }
      , { field: 'roleDescribe', title: '描述', width: '45%', align: 'center', unresize: true }
      , { fixed: 'right', title: '操作', width: '10%', align: 'center', toolbar: '#toolbar', unresize: true }
    ]]
  });
  
  //监听工具条
  table.on('tool(LAY-privilege)', function(obj){
    var data = obj.data;
    if(obj.event === 'del'){
      layer.confirm('确定删除此分类？', function(index){
        obj.del();
        layer.close(index);
      });
    } else if(obj.event === 'edit'){
      var tr = $(obj.tr);
      layer.open({
        type: 2
        ,title: '编辑分类'
        ,content: 'privilegeForm.html?upid='+ data.upid
        ,area: ['550px', '300px']
        ,btn: ['确定', '取消']
        ,yes: function(index, layero){
          //获取iframe元素的值
          var othis = layero.find('iframe').contents().find("#layuiadmin-app-form-tags")
          ,tags = othis.find('input[name="tags"]').val();
          
          if(!tags.replace(/\s/g, '')) return;
          
          obj.update({
            tags: tags
          });
          layer.close(index);
        }
        ,success: function(layero, index){
          //给iframe元素赋值
          var othis = layero.find('iframe').contents().find("#layuiadmin-app-form-tags").click();
          othis.find('input[name="tags"]').val(data.tags);
        }
      });
    }
  });
  //故障维修记录
  table.render({
    elem: '#faultRepairRecord'
    ,url: http_proxy('/alertManager/breakdownMtRecord') //模拟接口
    ,parseData:function(res){
      return{
        "code":0
        ,"count":res.total
        ,"data":res.rows
        ,'msg':''
      }
    }
    ,cols: [[
       {field: 'taskNum', width: 100,align: 'center', title: '任务号'}
      ,{field: 'eventCode', title: '设备编号', minWidth: 100,align: 'center'}
      ,{field: 'eventName', title: '设备名称',align: 'center'}
      ,{field: 'maintainWay', width: 100,align: 'center', title: '维修方式'}
      ,{field: 'maintainDate', align: 'center',title: '维修日期', minWidth: 100}
      ,{field: 'maintainPerson',align: 'center', title: '维修人'}
      ,{field: 'maintainContent',align: 'center', title: '维修内容'}
      ,{field: 'describe', align: 'center', title: '描述'}
      ,{title: '工单状态', minWidth: 100,align: 'center',templet:'#taskStatus'}
      ,{ fixed: 'right', title: '操作', width: '10%', align: 'center', toolbar: '#faultOperate', unresize: true }
     
    ]]
    ,page: true
    ,limit: 10
    ,limits: [10, 15, 20, 25, 30]
    ,text: '暂无数据'
    ,done:function(res){
      tdTitle();
    }
  });
  table.on('tool(faultRepairRecord)', function(obj){
    var data = obj.data;
    if(obj.event === 'detail'){
      layer.open({
        type: 2
        ,title: '设备详情'
        ,content: 'faultRepairRecordDetail.html?taskNum='+ data.taskNum
        ,maxmin: true
        ,area: ['80%', '80%']
        ,btn: ['确定', '取消']
        ,success: function(layero, index){
          let iframe = window['layui-layer-iframe'+index];
          iframe.initData(data);
        }
        ,yes: function(index, layero){
          var iframeWindow = window['layui-layer-iframe'+ index]
          ,submit = layero.find('iframe').contents().find('#planSubmit');
          //监听提交
          iframeWindow.layui.form.on('submit(planSubmit)', function(data){
            planTableIns.reload();
            layer.close(index); //关闭弹层
          });
          submit.trigger('click');
        }
      })
    }

  })
  
  //监听工具条
  table.on('tool(LAY-user)', function(obj){
    var data = obj.data;
    if(obj.event === 'del'){
      layer.confirm('确定删除此用户？', function(index){
        obj.del();
        layer.close(index);
      });
    } else if(obj.event === 'edit'){
      layer.open({
        type: 2
        ,title: '编辑'
        ,content: 'userForm.html?uid='+ data.uid
        ,maxmin: true
        ,area: ['550px', '550px']
        ,btn: ['确定', '取消']
        ,yes: function(index, layero){
          var iframeWindow = window['layui-layer-iframe'+ index]
          ,submit = layero.find('iframe').contents().find("#layuiadmin-app-form-edit");

          //监听提交
          iframeWindow.layui.form.on('submit(layuiadmin-app-form-edit)', function(data){
            var field = data.field; //获取提交的字段
            
            //提交 Ajax 成功后，静态更新表格中的数据
            //$.ajax({});              
            obj.update({
              label: field.label
              ,title: field.title
              ,author: field.author
              ,status: field.status
            }); //数据更新
            
            form.render();
            layer.close(index); //关闭弹层
          });  
          
          submit.trigger('click');
        }
      });
    }
  });

  exports('alarmManagement', {})
});